﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using JiebaNet.Segmenter.Common;
using JiebaNet.Segmenter.Properties;

namespace JiebaNet.Segmenter
{
    public class ConfigManager
    {
        private static string _configFileBaseDir = null;

        public static string ConfigFileBaseDir
        {
            get
            {
                if (_configFileBaseDir.IsNull())
                {
                    var configFileDir = "Resources";
                    if (!Path.IsPathRooted(configFileDir))
                    {
                        var domainDir = AppDomain.CurrentDomain.BaseDirectory;
                        configFileDir = Path.GetFullPath(Path.Combine(domainDir, configFileDir));
                    }
                    _configFileBaseDir = configFileDir;
                }

                return _configFileBaseDir;
            }
            set { _configFileBaseDir = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static Stream GetStream(string fileName)
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceStream($"JiebaNet.Segmenter.Resources.{fileName}");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static string ReadResource(string fileName)
        {
            using (Stream stream = GetStream(fileName))
            {
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        public static Stream GetMainDictFileStream()
        {
            return GetStream("dict.txt");
        }

        public static string[] GetMainDictFileLines()
        {
            List<string> results = new List<string>();
            using (StreamReader reader = new StreamReader(GetStream("dict.txt")))
            {
                string line;
                while((line = reader.ReadLine()) != null)
                {
                    results.Add(line);
                }
            }
            return results.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadProbTransFile()
        {
            return ReadResource("prob_trans.json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadProbEmitFile()
        {
            return ReadResource("prob_emit.json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadPosProbStartFile()
        {
            return ReadResource("pos_prob_start.json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadPosProbTransFile()
        {
            return ReadResource("pos_prob_trans.json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadPosProbEmitFile()
        {
            return ReadResource("pos_prob_emit.json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ReadCharStateTabFile()
        {
            return ReadResource("char_state_tab.json");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Stream GetIdfFileStream()
        {
            return GetStream("idf.txt");
        }

        public static string StopWordsFile => Path.Combine(ConfigFileBaseDir, "stopwords.txt");

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Stream GetStopWordsFileStream()
        {
            return GetStream("stopwords.txt");
        }
    }
}